var app = getApp();
var util = require("../../utils/util.js");
var siteinfo = require('../../siteinfo.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myShowType:-1,
    myShowTypeDiv:'display: none',
    tj: ["", "", "", "", "", ""],
    typeList: [
      {
        id: 0,
        name: '热门'
      },
      {
        id: 1,
        name: '最新'
      }
    ],
    inputShowed: false,
    list: [],
    no_data: false, //“没有数据”的变量，默认false，隐藏
    reachLoading: true, //"上拉加载"的变量，默认false，隐藏
    pageNumber: 1, //第几页,第几次加载
    pageSize: 10, //需要返回数据的个数
    name: '',
    type: 0,
    goodsList: '',
    searchStatus: false,
    user_id: '',
    filtrate: false,
    background: '#eee',
    color: '#333',
    select: '',
    priceMin: '',
    priceMax: '',
    price_flag: '',
    priceFlag: "价格由低到高",
    education: [
      {
        id: 0,
        name: '不限'
      },
      {
        id: 1,
        name: '初中'
      },
      {
        id: 2,
        name: '高中'
      },
      {
        id: 3,
        name: '大专'
      },
      {
        id: 4,
        name: '本科'
      },
      {
        id: 5,
        name: '硕士研究生'
      },
      {
        id: 6,
        name: '博士研究生'
      },
    ],//学历
    arr: [
      {
        id: 0,
        name: '不限'
      },
      {
        id: 1,
        name: '育婴师'
      },
      {
        id: 2,
        name: '月嫂'
      },
      {
        id: 3,
        name: '催乳师'
      },
      {
        id: 4,
        name: '养老护理'
      },
      {
        id: 5,
        name: '收纳师'
      },
      {
        id: 6,
        name: '中西面点'
      },
      {
        id: 7,
        name: '育儿嫂'
      }
    ],//类型
    experience: [
      {
        id: 0,
        name: '不限'
      },
      {
        id: 1,
        name: '2年'
      },
      {
        id: 2,
        name: '3年'
      },
      {
        id: 3,
        name: '5年'
      },
      {
        id: 4,
        name: '10年以上'
      }
    ]//工作经验
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var _this = this;
    _this.getDataList()

  },
  filterTab: function (e) {
    var data = [true, true, true], index = e.currentTarget.dataset.index;
    data[index] = !this.data.tab[index];
    this.setData({
      tab: data
    })
  },
  // 显示隐藏 div
  setMyShowTypeDiv:function(){
    const query = wx.createSelectorQuery();
    const _this=this
    query.select('.mySticky').boundingClientRect(function(rect){ 
      _this.setData({
        myShowTypeDiv:_this.data.myShowType>0?'display:block;top:'+(rect.top + 30)+'px':'display: none'
      })
    }).exec();   
  },
  setMyShowType:function(event){     
      wx.setPageStyle({
        style: {
          overflow: this.data.myShowType==event.currentTarget.dataset.index?'visible':'hidden'
        }
     })  
    this.setData({
      myShowType:this.data.myShowType==event.currentTarget.dataset.index?-1:event.currentTarget.dataset.index,
    }) 
    this.setMyShowTypeDiv()
  },
  closeShowType:function(event){ 
    this.setData({
      myShowType:-1
    }) 
    this.setMyShowTypeDiv()
    wx.setPageStyle({
      style: {
        overflow: 'visible'
      }
   })
  },
  phoneCall: function (e) {
    wx.makePhoneCall({
    //   phoneNumber: '18175118135',
    phoneNumber: '19918316230',
      success: function () {
      },
    })
  },
  //加载数据
  getDataList: function () {
    wx.setPageStyle({
      style: {
        overflow: 'visible'
      }
   })
    var _this = this
    _this.setData({
      pageNumber: 1,
      no_data: false
    }); 
    _this.getData()
  },
  getData:function(){
    var _this = this
    app._get(siteinfo.recruimeList, {
      name: _this.data.name,
      type: _this.data.type,
      pageSize: _this.data.pageSize,
      pageNumber: _this.data.pageNumber,
      experience: _this.data.tj[1],
      education: _this.data.tj[2],
      arr: _this.data.tj[3]
    }, function (result) {
      // _this.setData({
      //   list: result.data
      // });
      // console.log('list', _this.data.list);

      var agelist = result.data 
      var oldList=_this.data.pageNumber>1?_this.data.list:[]
      agelist.forEach(function (item, index) {
        let ages = item.age
        let age =null==ages?'':util.getAge(ages)
        item.age = age 
      })
      _this.setData({
        // agelist: result.data,
        list:[...oldList,...agelist]
      });

      if (result.data.size==0) {
        _this.setData({
          no_data: true
        });
      }else if(_this.data.no_data){
        _this.setData({
          no_data: false
        });
      }
      wx.stopPullDownRefresh();
    })
  },
  show: function () {
    this.setData({
      filtrate: true
    })
  },
  none: function () {
    this.setData({
      filtrate: false
    })
  },
  selectBtn: function (e) {
    var currentTj = this.data.tj
    //存在取消
    if(currentTj[e.currentTarget.dataset.type]==e.currentTarget.dataset.name){
      currentTj[e.currentTarget.dataset.type]=''
    }else{
      currentTj[e.currentTarget.dataset.type] = e.currentTarget.dataset.name; 
    }
    this.setData({
      tj: currentTj,
      myShowType:-1
    }) 
    this.setMyShowTypeDiv()
    this.getDataList();
  },
  /**
 * 选中分类
 */

  getleft: function (t) {
    let type = t.target.dataset.id
    this.setData({
      type
    });
    this.getDataList()
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      name: "",
      inputShowed: false
    });
    this.getDataList()
  },
  clearInput: function () {
    this.setData({
      name: ""
    });
    this.getDataList()
  },
  inputTyping: function (e) {
    this.setData({
      name: e.detail.value,
      no_data: false,
      pageNumber: 1,
      pageSize: 10
    });
    this.getDataList()
  },
  onPullDownRefresh: function () {
    var _this = this
    _this.getDataList()
  },
  onPageScroll: function (e) {
    if (this.data.allHeight - this.data.clientHight === e.scrollTop) {   // 判断是否滚动动到底部
      if (!this.data.no_data) {
        const pageNumber = this.data.pageNumber + 1
        this.setData({
          pageNumber: pageNumber
        })
        this.getDataList(app.globalData.latitude, app.globalData.longitude)
      }
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () { 
    this.searchScrollLower();
  },
  searchScrollLower: function () {
    let _this = this;
    if (_this.data.reachLoading && !_this.data.no_data) {
      _this.setData({
        pageNumber: _this.data.pageNumber + 1,
      });
      //调接口
      _this.getData()
    }
  },
  /**
 * 返回顶部
 */
  goTop: function (e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  onPageScroll: function (e) {
    this.setData({
      floorstatus: e.scrollTop > 200
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */

   

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})